<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	
	<link rel="stylesheet" href="css/jquery.mobile-1.3.1.css" />
	
	<script type="text/javascript" src="js/jquery-1.9.1.js"></script>	
	<script type="text/javascript" src="js/jquery.mobile-1.3.0.js"></script>
	<script type="text/javascript" src="js/cordova-2.5.0.js" charset="utf-8" ></script>

	<script type="text/javascript" charset="utf-8">	
		function iniciar() {			
			document.addEventListener("deviceready", dispositivoListo, false);			
		}
		function dispositivoListo(){		
			
		}	
		function buscarContactos(){
			var searchStr     =	document.getElementById("txtContacto").value;			
			var searchScope   = document.getElementById("lstCriterio").selectedIndex;			
			var contactFields = [];
			
			switch(searchScope) {
				case 1:					
					contactFields = ['name', 'streetAddress', 'locality', 'region', 'postalCode', 'country'];
					break;
				case 2:
					contactFields = ['displayName', 'name', 'phoneNumbers', 'emails'];
					break;
				case 3:
					contactFields = ['name', 'phoneNumbers'];
					break;
				default:
					contactFields = ['*'];
			}

			var searchOptions = { filter : searchStr, multiple : true };
			navigator.contacts.find(contactFields, onContactSearchSuccess, onContactSearchError, searchOptions);
		}
		
		var contactList;
		
		function onContactSearchSuccess(contacts) {
			var i, len, theList;
			contactList = contacts;
			len = contacts.length;
			
			if(len > 0) {
				theList = '<ul data-role="listview">';
				for( i = 0, len; i < len; i += 1) {			
					if(contacts[i].displayName == null) {
						theList += '<li><a onclick="showContact(' + i +	');">' + contacts[i].name.familyName + ", " + contacts[i].name.givenName + '</a></li>';
					} else {
						theList += '<li><a onclick="showContact(' + i + ');">' + contacts[i].displayName + '</a></li>';
					}
				}
				theList += '</ul>';
				$('#pContactos').html(theList);			
				//$.mobile.changePage("#contactList", "slide", false, true);
			} else {
				navigator.notification.alert('Search returned 0 results', null, 'Contact Search');
			}
		}
		
		function onContactSearchError( e ){
			navigator.notification.alert( "Error: " + e );
		}
		
		
	</script>
</head>

<body onload="iniciar()">

	<div data-role="page">
		<div data-role="header" data-position="fixed">		
			<h1>PI PhoneGap</h1>
		</div>
		
		<div data-role="content">
			<p>Ejemplo de Contactos</p>
			
			<form>
				<div data-role="fieldcontain">
					<label for="txtContacto">Buscar contacto:</label>
					<input type="search" name="txtContacto" id="txtContacto" placeholder="Ingrese aquí"/>
				</div>
				<div data-role="fieldcontain">
					<label for="lstCriterio">¿Qué datos desea ver?</label>
					<select name="lstCriterio" id="lstCriterio" data-mini="true">
				        <option value="1">Todos</option>
				        <option value="2">Nombre</option>
				        <option value="3">Dirección</option>
				    </select>
				</div>
				 <input type="button" value="Buscar" onclick="buscarContactos()"/>				
			</form>
			
			<p id="pContactos"></p>
		</div>
		
		<div data-role="footer" data-position="fixed">
			<h1>RCondezo &copy; 2013</h1>
		</div>
	</div>
	
</body>

</html>